package vn.hanelsoft.newsduet.view;

import vn.hanelsoft.newsduet.R;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;


@SuppressLint("SetJavaScriptEnabled")
public class ShareTwitterDialog extends Dialog{

	private String mUrl;
	private WebView mWebView;
	private ProgressDialog mSpinner;

	public ShareTwitterDialog(Context ctx, String url) {
		super(ctx, android.R.style.Theme_Translucent_NoTitleBar);
		mUrl = url;
	}
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSpinner = new ProgressDialog(getContext());
        mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mSpinner.setMessage("Loading...");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.share_twitter);
		setUpWebView();
		
		//TextView txtUrl = (TextView) findViewById(R.id.url);
		//txtUrl.setText(mUrl);
		/*Button btnDone = (Button) findViewById(R.id.btnTwitterDone);
		btnDone.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ShareTwitterDialog.this.dismiss();
			}
		});*/
	}
	
	private void setUpWebView() {
        //LinearLayout webViewContainer = new LinearLayout(getContext());
        mWebView = (WebView)findViewById(R.id.wvTwitter);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new TwitterWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setUserAgentString("silly_that_i_have_to_do_this");
        mWebView.loadUrl(mUrl);
        //mWebView.setLayoutParams(FILL);
        
    }
	
    private class TwitterWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        	
    		view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        	
            super.onReceivedError(view, errorCode, description, failingUrl);
            
            ShareTwitterDialog.this.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mSpinner.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            mSpinner.dismiss();
            super.onPageFinished(view, url);
        }

    }
    
}
